<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a name="line.1"></a>
<span class="sourceLineNo">002</span>/*<a name="line.2"></a>
<span class="sourceLineNo">003</span> * Copyright (C) 2014 Archie L. Cobbs. All rights reserved.<a name="line.3"></a>
<span class="sourceLineNo">004</span> *<a name="line.4"></a>
<span class="sourceLineNo">005</span> * $Id: AbstractKVNavigableSet.html 878 2015-04-17 01:32:03Z archie.cobbs $<a name="line.5"></a>
<span class="sourceLineNo">006</span> */<a name="line.6"></a>
<span class="sourceLineNo">007</span><a name="line.7"></a>
<span class="sourceLineNo">008</span>package org.jsimpledb.kv.util;<a name="line.8"></a>
<span class="sourceLineNo">009</span><a name="line.9"></a>
<span class="sourceLineNo">010</span>import java.util.Iterator;<a name="line.10"></a>
<span class="sourceLineNo">011</span>import java.util.NavigableSet;<a name="line.11"></a>
<span class="sourceLineNo">012</span><a name="line.12"></a>
<span class="sourceLineNo">013</span>import org.jsimpledb.kv.KVPair;<a name="line.13"></a>
<span class="sourceLineNo">014</span>import org.jsimpledb.kv.KVStore;<a name="line.14"></a>
<span class="sourceLineNo">015</span>import org.jsimpledb.kv.KeyFilter;<a name="line.15"></a>
<span class="sourceLineNo">016</span>import org.jsimpledb.kv.KeyFilterUtil;<a name="line.16"></a>
<span class="sourceLineNo">017</span>import org.jsimpledb.kv.KeyRange;<a name="line.17"></a>
<span class="sourceLineNo">018</span>import org.jsimpledb.util.AbstractNavigableSet;<a name="line.18"></a>
<span class="sourceLineNo">019</span>import org.jsimpledb.util.Bounds;<a name="line.19"></a>
<span class="sourceLineNo">020</span>import org.jsimpledb.util.ByteReader;<a name="line.20"></a>
<span class="sourceLineNo">021</span>import org.jsimpledb.util.ByteUtil;<a name="line.21"></a>
<span class="sourceLineNo">022</span>import org.jsimpledb.util.ByteWriter;<a name="line.22"></a>
<span class="sourceLineNo">023</span><a name="line.23"></a>
<span class="sourceLineNo">024</span>/**<a name="line.24"></a>
<span class="sourceLineNo">025</span> * {@link java.util.NavigableSet} support superclass for sets backed by elements encoded as {@code byte[]}<a name="line.25"></a>
<span class="sourceLineNo">026</span> * keys in a {@link KVStore} and whose sort order is consistent with their {@code byte[]} key encoding.<a name="line.26"></a>
<span class="sourceLineNo">027</span> * There must be an equivalence between elements and {@code byte[]} key encodings (i.e., there must be<a name="line.27"></a>
<span class="sourceLineNo">028</span> * only one valid encoding per set element).<a name="line.28"></a>
<span class="sourceLineNo">029</span> *<a name="line.29"></a>
<span class="sourceLineNo">030</span> * &lt;p&gt;&lt;b&gt;Subclass Methods&lt;/b&gt;&lt;/p&gt;<a name="line.30"></a>
<span class="sourceLineNo">031</span> *<a name="line.31"></a>
<span class="sourceLineNo">032</span> * &lt;p&gt;<a name="line.32"></a>
<span class="sourceLineNo">033</span> * Subclasses must implement the {@linkplain #encode(ByteWriter, Object) encode()} and {@linkplain #decode decode()}<a name="line.33"></a>
<span class="sourceLineNo">034</span> * methods to convert elements to/from {@code byte[]} keys (associated values are ignored), and<a name="line.34"></a>
<span class="sourceLineNo">035</span> * {@link #createSubSet(boolean, KeyRange, KeyFilter, Bounds) createSubSet()}<a name="line.35"></a>
<span class="sourceLineNo">036</span> * to allow creating reversed and restricted range sub-sets.<a name="line.36"></a>
<span class="sourceLineNo">037</span> * &lt;/p&gt;<a name="line.37"></a>
<span class="sourceLineNo">038</span> *<a name="line.38"></a>
<span class="sourceLineNo">039</span> * &lt;p&gt;<a name="line.39"></a>
<span class="sourceLineNo">040</span> * Subclasses must also implement {@link #comparator comparator()}, and the resulting sort order must be consistent with<a name="line.40"></a>
<span class="sourceLineNo">041</span> * the sort order of the encoded {@code byte[]} keys (possibly {@link #reversed}).<a name="line.41"></a>
<span class="sourceLineNo">042</span> * &lt;/p&gt;<a name="line.42"></a>
<span class="sourceLineNo">043</span> *<a name="line.43"></a>
<span class="sourceLineNo">044</span> * &lt;p&gt;<a name="line.44"></a>
<span class="sourceLineNo">045</span> * This class provides a read-only implementation; for a mutable implementation, subclasses should also implement<a name="line.45"></a>
<span class="sourceLineNo">046</span> * {@link #add add()} (if appropriate), {@link #remove remove()}, and {@link #clear}; note, these methods must verify<a name="line.46"></a>
<span class="sourceLineNo">047</span> * the key {@link #isVisible isVisible()} before making any changes.<a name="line.47"></a>
<span class="sourceLineNo">048</span> * &lt;/p&gt;<a name="line.48"></a>
<span class="sourceLineNo">049</span> *<a name="line.49"></a>
<span class="sourceLineNo">050</span> * &lt;p&gt;<a name="line.50"></a>
<span class="sourceLineNo">051</span> * Additional subclass notes:<a name="line.51"></a>
<span class="sourceLineNo">052</span> * &lt;ul&gt;<a name="line.52"></a>
<span class="sourceLineNo">053</span> *  &lt;li&gt;{@link #iterator} returns an {@link Iterator} whose {@link Iterator#remove Iterator.remove()} method delegates<a name="line.53"></a>
<span class="sourceLineNo">054</span> *      to this instance's {@link #remove remove()} method.<a name="line.54"></a>
<span class="sourceLineNo">055</span> * &lt;/ul&gt;<a name="line.55"></a>
<span class="sourceLineNo">056</span> *<a name="line.56"></a>
<span class="sourceLineNo">057</span> * &lt;p&gt;&lt;b&gt;Prefix Mode&lt;/b&gt;&lt;/p&gt;<a name="line.57"></a>
<span class="sourceLineNo">058</span> *<a name="line.58"></a>
<span class="sourceLineNo">059</span> * &lt;p&gt;<a name="line.59"></a>
<span class="sourceLineNo">060</span> * Instances support a "prefix mode" where the {@code byte[]} keys may have arbitrary trailing garbage, which is ignored,<a name="line.60"></a>
<span class="sourceLineNo">061</span> * and so by definition no key can be a prefix of any other key. The length of the prefix is determined implicitly by the<a name="line.61"></a>
<span class="sourceLineNo">062</span> * number of bytes produced by {@link #encode encode()} or consumed by {@link #decode decode()}.<a name="line.62"></a>
<span class="sourceLineNo">063</span> * When not in prefix mode, {@link #decode decode()} &lt;b&gt;must&lt;/b&gt; consume the entire key to preserve correct semantics.<a name="line.63"></a>
<span class="sourceLineNo">064</span> * &lt;/p&gt;<a name="line.64"></a>
<span class="sourceLineNo">065</span> *<a name="line.65"></a>
<span class="sourceLineNo">066</span> * &lt;p&gt;&lt;b&gt;Key Restrictions&lt;/b&gt;&lt;/p&gt;<a name="line.66"></a>
<span class="sourceLineNo">067</span> *<a name="line.67"></a>
<span class="sourceLineNo">068</span> * &lt;p&gt;<a name="line.68"></a>
<span class="sourceLineNo">069</span> * Instances are configured with an (optional) {@link KeyRange}; when {@linkplain #bounds range restriction} is in<a name="line.69"></a>
<span class="sourceLineNo">070</span> * effect, this key range corresponds to the bounds.<a name="line.70"></a>
<span class="sourceLineNo">071</span> * &lt;/p&gt;<a name="line.71"></a>
<span class="sourceLineNo">072</span> *<a name="line.72"></a>
<span class="sourceLineNo">073</span> * &lt;p&gt;<a name="line.73"></a>
<span class="sourceLineNo">074</span> * Instances also support filtering visible values using a {@link KeyFilter}; see {@link #filterKeys filterKeys()}.<a name="line.74"></a>
<span class="sourceLineNo">075</span> * To be {@linkplain #isVisible} in the set, keys must both be in the {@link KeyRange} and pass the {@link KeyFilter}.<a name="line.75"></a>
<span class="sourceLineNo">076</span> * &lt;/p&gt;<a name="line.76"></a>
<span class="sourceLineNo">077</span> *<a name="line.77"></a>
<span class="sourceLineNo">078</span> * &lt;p&gt;&lt;b&gt;Concurrent Modifications&lt;/b&gt;&lt;/p&gt;<a name="line.78"></a>
<span class="sourceLineNo">079</span> *<a name="line.79"></a>
<span class="sourceLineNo">080</span> * &lt;p&gt;<a name="line.80"></a>
<span class="sourceLineNo">081</span> * This implementation never throws {@link java.util.ConcurrentModificationException}; instead, iterators always<a name="line.81"></a>
<span class="sourceLineNo">082</span> * see the most up-to-date state of the associated {@link KVStore}.<a name="line.82"></a>
<span class="sourceLineNo">083</span> * &lt;/p&gt;<a name="line.83"></a>
<span class="sourceLineNo">084</span> *<a name="line.84"></a>
<span class="sourceLineNo">085</span> * @see AbstractKVNavigableMap<a name="line.85"></a>
<span class="sourceLineNo">086</span> * @param &lt;E&gt; element type<a name="line.86"></a>
<span class="sourceLineNo">087</span> */<a name="line.87"></a>
<span class="sourceLineNo">088</span>public abstract class AbstractKVNavigableSet&lt;E&gt; extends AbstractNavigableSet&lt;E&gt; {<a name="line.88"></a>
<span class="sourceLineNo">089</span><a name="line.89"></a>
<span class="sourceLineNo">090</span>    /**<a name="line.90"></a>
<span class="sourceLineNo">091</span>     * The underlying {@link KVStore}.<a name="line.91"></a>
<span class="sourceLineNo">092</span>     */<a name="line.92"></a>
<span class="sourceLineNo">093</span>    protected final KVStore kv;<a name="line.93"></a>
<span class="sourceLineNo">094</span><a name="line.94"></a>
<span class="sourceLineNo">095</span>    /**<a name="line.95"></a>
<span class="sourceLineNo">096</span>     * Whether we are in "prefix" mode.<a name="line.96"></a>
<span class="sourceLineNo">097</span>     */<a name="line.97"></a>
<span class="sourceLineNo">098</span>    protected final boolean prefixMode;<a name="line.98"></a>
<span class="sourceLineNo">099</span><a name="line.99"></a>
<span class="sourceLineNo">100</span>    /**<a name="line.100"></a>
<span class="sourceLineNo">101</span>     * Whether the ordering of this instance is reversed.<a name="line.101"></a>
<span class="sourceLineNo">102</span>     */<a name="line.102"></a>
<span class="sourceLineNo">103</span>    protected final boolean reversed;<a name="line.103"></a>
<span class="sourceLineNo">104</span><a name="line.104"></a>
<span class="sourceLineNo">105</span>    /**<a name="line.105"></a>
<span class="sourceLineNo">106</span>     * Key range, or null for the entire range.<a name="line.106"></a>
<span class="sourceLineNo">107</span>     */<a name="line.107"></a>
<span class="sourceLineNo">108</span>    protected final KeyRange keyRange;<a name="line.108"></a>
<span class="sourceLineNo">109</span><a name="line.109"></a>
<span class="sourceLineNo">110</span>    /**<a name="line.110"></a>
<span class="sourceLineNo">111</span>     * Key filter, or null if all keys in the key range should be visible.<a name="line.111"></a>
<span class="sourceLineNo">112</span>     */<a name="line.112"></a>
<span class="sourceLineNo">113</span>    protected final KeyFilter keyFilter;<a name="line.113"></a>
<span class="sourceLineNo">114</span><a name="line.114"></a>
<span class="sourceLineNo">115</span>// Constructors<a name="line.115"></a>
<span class="sourceLineNo">116</span><a name="line.116"></a>
<span class="sourceLineNo">117</span>    /**<a name="line.117"></a>
<span class="sourceLineNo">118</span>     * Convenience constructor for when there are no range restrictions.<a name="line.118"></a>
<span class="sourceLineNo">119</span>     *<a name="line.119"></a>
<span class="sourceLineNo">120</span>     * @param kv underlying {@link KVStore}<a name="line.120"></a>
<span class="sourceLineNo">121</span>     * @param prefixMode whether to allow keys to have trailing garbage<a name="line.121"></a>
<span class="sourceLineNo">122</span>     * @throws IllegalArgumentException if {@code kv} is null<a name="line.122"></a>
<span class="sourceLineNo">123</span>     */<a name="line.123"></a>
<span class="sourceLineNo">124</span>    protected AbstractKVNavigableSet(KVStore kv, boolean prefixMode) {<a name="line.124"></a>
<span class="sourceLineNo">125</span>        this(kv, prefixMode, (KeyRange)null);<a name="line.125"></a>
<span class="sourceLineNo">126</span>    }<a name="line.126"></a>
<span class="sourceLineNo">127</span><a name="line.127"></a>
<span class="sourceLineNo">128</span>    /**<a name="line.128"></a>
<span class="sourceLineNo">129</span>     * Convenience constructor for when the range of visible {@link KVStore} keys is all keys sharing a given {@code byte[]} prefix.<a name="line.129"></a>
<span class="sourceLineNo">130</span>     *<a name="line.130"></a>
<span class="sourceLineNo">131</span>     * @param kv underlying {@link KVStore}<a name="line.131"></a>
<span class="sourceLineNo">132</span>     * @param prefixMode whether to allow keys to have trailing garbage<a name="line.132"></a>
<span class="sourceLineNo">133</span>     * @param prefix prefix defining minimum and maximum keys<a name="line.133"></a>
<span class="sourceLineNo">134</span>     * @throws IllegalArgumentException if {@code kv} is null<a name="line.134"></a>
<span class="sourceLineNo">135</span>     * @throws IllegalArgumentException if {@code prefix} is null or empty<a name="line.135"></a>
<span class="sourceLineNo">136</span>     */<a name="line.136"></a>
<span class="sourceLineNo">137</span>    protected AbstractKVNavigableSet(KVStore kv, boolean prefixMode, byte[] prefix) {<a name="line.137"></a>
<span class="sourceLineNo">138</span>        this(kv, prefixMode, KeyRange.forPrefix(prefix));<a name="line.138"></a>
<span class="sourceLineNo">139</span>    }<a name="line.139"></a>
<span class="sourceLineNo">140</span><a name="line.140"></a>
<span class="sourceLineNo">141</span>    /**<a name="line.141"></a>
<span class="sourceLineNo">142</span>     * Primary constructor.<a name="line.142"></a>
<span class="sourceLineNo">143</span>     *<a name="line.143"></a>
<span class="sourceLineNo">144</span>     * @param kv underlying {@link KVStore}<a name="line.144"></a>
<span class="sourceLineNo">145</span>     * @param prefixMode whether to allow keys to have trailing garbage<a name="line.145"></a>
<span class="sourceLineNo">146</span>     * @param keyRange key range restriction, or null for none<a name="line.146"></a>
<span class="sourceLineNo">147</span>     * @throws IllegalArgumentException if {@code kv} is null<a name="line.147"></a>
<span class="sourceLineNo">148</span>     */<a name="line.148"></a>
<span class="sourceLineNo">149</span>    protected AbstractKVNavigableSet(KVStore kv, boolean prefixMode, KeyRange keyRange) {<a name="line.149"></a>
<span class="sourceLineNo">150</span>        this(kv, prefixMode, false, keyRange, null, new Bounds&lt;E&gt;());<a name="line.150"></a>
<span class="sourceLineNo">151</span>    }<a name="line.151"></a>
<span class="sourceLineNo">152</span><a name="line.152"></a>
<span class="sourceLineNo">153</span>    /**<a name="line.153"></a>
<span class="sourceLineNo">154</span>     * Internal constructor. Used for creating sub-sets and reversed views.<a name="line.154"></a>
<span class="sourceLineNo">155</span>     *<a name="line.155"></a>
<span class="sourceLineNo">156</span>     * &lt;p&gt;<a name="line.156"></a>
<span class="sourceLineNo">157</span>     * Note: if {@code bounds} are set, then {@code keyRange} must exclude all keys outside of those bounds.<a name="line.157"></a>
<span class="sourceLineNo">158</span>     * &lt;/p&gt;<a name="line.158"></a>
<span class="sourceLineNo">159</span>     *<a name="line.159"></a>
<span class="sourceLineNo">160</span>     * @param kv underlying {@link KVStore}<a name="line.160"></a>
<span class="sourceLineNo">161</span>     * @param prefixMode whether to allow keys to have trailing garbage<a name="line.161"></a>
<span class="sourceLineNo">162</span>     * @param reversed whether ordering is reversed (implies {@code bounds} are also inverted, but &lt;i&gt;not&lt;/i&gt; {@code keyRange})<a name="line.162"></a>
<span class="sourceLineNo">163</span>     * @param keyRange key range restriction, or null for none<a name="line.163"></a>
<span class="sourceLineNo">164</span>     * @param keyFilter key filter, or null for none<a name="line.164"></a>
<span class="sourceLineNo">165</span>     * @param bounds range restriction<a name="line.165"></a>
<span class="sourceLineNo">166</span>     * @throws IllegalArgumentException if {@code kv} or {@code bounds} is null<a name="line.166"></a>
<span class="sourceLineNo">167</span>     */<a name="line.167"></a>
<span class="sourceLineNo">168</span>    protected AbstractKVNavigableSet(KVStore kv, boolean prefixMode, boolean reversed,<a name="line.168"></a>
<span class="sourceLineNo">169</span>      KeyRange keyRange, KeyFilter keyFilter, Bounds&lt;E&gt; bounds) {<a name="line.169"></a>
<span class="sourceLineNo">170</span>        super(bounds);<a name="line.170"></a>
<span class="sourceLineNo">171</span>        if (kv == null)<a name="line.171"></a>
<span class="sourceLineNo">172</span>            throw new IllegalArgumentException("null kv");<a name="line.172"></a>
<span class="sourceLineNo">173</span>        this.kv = kv;<a name="line.173"></a>
<span class="sourceLineNo">174</span>        this.prefixMode = prefixMode;<a name="line.174"></a>
<span class="sourceLineNo">175</span>        this.reversed = reversed;<a name="line.175"></a>
<span class="sourceLineNo">176</span>        this.keyRange = keyRange;<a name="line.176"></a>
<span class="sourceLineNo">177</span>        this.keyFilter = keyFilter;<a name="line.177"></a>
<span class="sourceLineNo">178</span>    }<a name="line.178"></a>
<span class="sourceLineNo">179</span><a name="line.179"></a>
<span class="sourceLineNo">180</span>    @Override<a name="line.180"></a>
<span class="sourceLineNo">181</span>    @SuppressWarnings("unchecked")<a name="line.181"></a>
<span class="sourceLineNo">182</span>    public boolean contains(Object obj) {<a name="line.182"></a>
<span class="sourceLineNo">183</span><a name="line.183"></a>
<span class="sourceLineNo">184</span>        // Encode key and check visibility<a name="line.184"></a>
<span class="sourceLineNo">185</span>        final byte[] key = this.encodeVisible(obj, false);<a name="line.185"></a>
<span class="sourceLineNo">186</span>        if (key == null)<a name="line.186"></a>
<span class="sourceLineNo">187</span>            return false;<a name="line.187"></a>
<span class="sourceLineNo">188</span><a name="line.188"></a>
<span class="sourceLineNo">189</span>        // Find key, or some longer key with the same prefix in prefix mode<a name="line.189"></a>
<span class="sourceLineNo">190</span>        final KVPair pair;<a name="line.190"></a>
<span class="sourceLineNo">191</span>        if (this.prefixMode) {<a name="line.191"></a>
<span class="sourceLineNo">192</span>            pair = this.kv.getAtLeast(key);<a name="line.192"></a>
<span class="sourceLineNo">193</span>            return pair != null &amp;&amp; ByteUtil.isPrefixOf(key, pair.getKey());<a name="line.193"></a>
<span class="sourceLineNo">194</span>        } else<a name="line.194"></a>
<span class="sourceLineNo">195</span>            return this.kv.get(key) != null;<a name="line.195"></a>
<span class="sourceLineNo">196</span>    }<a name="line.196"></a>
<span class="sourceLineNo">197</span><a name="line.197"></a>
<span class="sourceLineNo">198</span>    @Override<a name="line.198"></a>
<span class="sourceLineNo">199</span>    public Iterator&lt;E&gt; iterator() {<a name="line.199"></a>
<span class="sourceLineNo">200</span>        return new AbstractKVIterator&lt;E&gt;(this.kv, this.prefixMode, this.reversed, this.keyRange, this.keyFilter) {<a name="line.200"></a>
<span class="sourceLineNo">201</span><a name="line.201"></a>
<span class="sourceLineNo">202</span>            @Override<a name="line.202"></a>
<span class="sourceLineNo">203</span>            protected E decodePair(KVPair pair, ByteReader keyReader) {<a name="line.203"></a>
<span class="sourceLineNo">204</span>                return AbstractKVNavigableSet.this.decode(keyReader);<a name="line.204"></a>
<span class="sourceLineNo">205</span>            }<a name="line.205"></a>
<span class="sourceLineNo">206</span><a name="line.206"></a>
<span class="sourceLineNo">207</span>            @Override<a name="line.207"></a>
<span class="sourceLineNo">208</span>            protected void doRemove(E value, KVPair pair) {<a name="line.208"></a>
<span class="sourceLineNo">209</span>                AbstractKVNavigableSet.this.remove(value);<a name="line.209"></a>
<span class="sourceLineNo">210</span>            }<a name="line.210"></a>
<span class="sourceLineNo">211</span>        };<a name="line.211"></a>
<span class="sourceLineNo">212</span>    }<a name="line.212"></a>
<span class="sourceLineNo">213</span><a name="line.213"></a>
<span class="sourceLineNo">214</span>    /**<a name="line.214"></a>
<span class="sourceLineNo">215</span>     * Create a view of this instance with additional filtering applied to the underlying {@code byte[]} encoded keys.<a name="line.215"></a>
<span class="sourceLineNo">216</span>     * Any set element for which the corresponding key does not pass {@code keyFilter} will be effectively hidden from view.<a name="line.216"></a>
<span class="sourceLineNo">217</span>     *<a name="line.217"></a>
<span class="sourceLineNo">218</span>     * &lt;p&gt;<a name="line.218"></a>
<span class="sourceLineNo">219</span>     * The restrictions of the given {@link KeyFilter} will be added to any current {@link KeyFilter} restrictions on this instance.<a name="line.219"></a>
<span class="sourceLineNo">220</span>     * The {@link #bounds} associated with this instance will not change.<a name="line.220"></a>
<span class="sourceLineNo">221</span>     * &lt;/p&gt;<a name="line.221"></a>
<span class="sourceLineNo">222</span>     *<a name="line.222"></a>
<span class="sourceLineNo">223</span>     * @param keyFilter additional key filtering to apply<a name="line.223"></a>
<span class="sourceLineNo">224</span>     * @return filtered view of this instance<a name="line.224"></a>
<span class="sourceLineNo">225</span>     * @throws IllegalArgumentException if {@code keyFilter} is null<a name="line.225"></a>
<span class="sourceLineNo">226</span>     */<a name="line.226"></a>
<span class="sourceLineNo">227</span>    public NavigableSet&lt;E&gt; filterKeys(KeyFilter keyFilter) {<a name="line.227"></a>
<span class="sourceLineNo">228</span>        if (keyFilter == null)<a name="line.228"></a>
<span class="sourceLineNo">229</span>            throw new IllegalArgumentException("null keyFilter");<a name="line.229"></a>
<span class="sourceLineNo">230</span>        if (this.keyFilter != null)<a name="line.230"></a>
<span class="sourceLineNo">231</span>            keyFilter = KeyFilterUtil.intersection(keyFilter, this.keyFilter);<a name="line.231"></a>
<span class="sourceLineNo">232</span>        return this.createSubSet(this.reversed, this.keyRange, keyFilter, this.bounds);<a name="line.232"></a>
<span class="sourceLineNo">233</span>    }<a name="line.233"></a>
<span class="sourceLineNo">234</span><a name="line.234"></a>
<span class="sourceLineNo">235</span>    @Override<a name="line.235"></a>
<span class="sourceLineNo">236</span>    protected boolean isWithinLowerBound(E elem) {<a name="line.236"></a>
<span class="sourceLineNo">237</span>        if (!super.isWithinLowerBound(elem))<a name="line.237"></a>
<span class="sourceLineNo">238</span>            return false;<a name="line.238"></a>
<span class="sourceLineNo">239</span>        if (this.keyRange == null)<a name="line.239"></a>
<span class="sourceLineNo">240</span>            return true;<a name="line.240"></a>
<span class="sourceLineNo">241</span>        final ByteWriter writer = new ByteWriter();<a name="line.241"></a>
<span class="sourceLineNo">242</span>        this.encode(writer, elem);<a name="line.242"></a>
<span class="sourceLineNo">243</span>        return KeyRange.compare(writer.getBytes(), this.keyRange.getMin()) &gt;= 0;<a name="line.243"></a>
<span class="sourceLineNo">244</span>    }<a name="line.244"></a>
<span class="sourceLineNo">245</span><a name="line.245"></a>
<span class="sourceLineNo">246</span>    @Override<a name="line.246"></a>
<span class="sourceLineNo">247</span>    protected boolean isWithinUpperBound(E elem) {<a name="line.247"></a>
<span class="sourceLineNo">248</span>        if (!super.isWithinUpperBound(elem))<a name="line.248"></a>
<span class="sourceLineNo">249</span>            return false;<a name="line.249"></a>
<span class="sourceLineNo">250</span>        if (this.keyRange == null)<a name="line.250"></a>
<span class="sourceLineNo">251</span>            return true;<a name="line.251"></a>
<span class="sourceLineNo">252</span>        final ByteWriter writer = new ByteWriter();<a name="line.252"></a>
<span class="sourceLineNo">253</span>        this.encode(writer, elem);<a name="line.253"></a>
<span class="sourceLineNo">254</span>        return KeyRange.compare(writer.getBytes(), this.keyRange.getMax()) &lt; 0;<a name="line.254"></a>
<span class="sourceLineNo">255</span>    }<a name="line.255"></a>
<span class="sourceLineNo">256</span><a name="line.256"></a>
<span class="sourceLineNo">257</span>    @Override<a name="line.257"></a>
<span class="sourceLineNo">258</span>    protected final NavigableSet&lt;E&gt; createSubSet(boolean reverse, Bounds&lt;E&gt; newBounds) {<a name="line.258"></a>
<span class="sourceLineNo">259</span><a name="line.259"></a>
<span class="sourceLineNo">260</span>        // Determine the direction of the new sub-set<a name="line.260"></a>
<span class="sourceLineNo">261</span>        final boolean newReversed = this.reversed ^ reverse;<a name="line.261"></a>
<span class="sourceLineNo">262</span><a name="line.262"></a>
<span class="sourceLineNo">263</span>        // Determine new min and max keys<a name="line.263"></a>
<span class="sourceLineNo">264</span>        final KeyRange newKeyRange = this.buildKeyRange(newReversed ? newBounds.reverse() : newBounds);<a name="line.264"></a>
<span class="sourceLineNo">265</span><a name="line.265"></a>
<span class="sourceLineNo">266</span>        // Create subset<a name="line.266"></a>
<span class="sourceLineNo">267</span>        return this.createSubSet(newReversed, newKeyRange, this.keyFilter, newBounds);<a name="line.267"></a>
<span class="sourceLineNo">268</span>    }<a name="line.268"></a>
<span class="sourceLineNo">269</span><a name="line.269"></a>
<span class="sourceLineNo">270</span>    /**<a name="line.270"></a>
<span class="sourceLineNo">271</span>     * Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds and<a name="line.271"></a>
<span class="sourceLineNo">272</span>     * the given {@link KeyFilter}, if any.<a name="line.272"></a>
<span class="sourceLineNo">273</span>     * The bounds are consistent with the reversed ordering (i.e., reversed if {@code reverse} is true)<a name="line.273"></a>
<span class="sourceLineNo">274</span>     * and have already been range-checked against this instance's bounds.<a name="line.274"></a>
<span class="sourceLineNo">275</span>     *<a name="line.275"></a>
<span class="sourceLineNo">276</span>     * @param newReversed whether the new set's ordering should be reversed (implies {@code newBounds} are also inverted,<a name="line.276"></a>
<span class="sourceLineNo">277</span>     *  but &lt;i&gt;not&lt;/i&gt; {@code keyRange}); note: means "absolutely" reversed, not relative to this instance<a name="line.277"></a>
<span class="sourceLineNo">278</span>     * @param newKeyRange new key range, or null for none; will be consistent with {@code bounds}, if any<a name="line.278"></a>
<span class="sourceLineNo">279</span>     * @param newKeyFilter new key filter, or null for none<a name="line.279"></a>
<span class="sourceLineNo">280</span>     * @param newBounds new bounds<a name="line.280"></a>
<span class="sourceLineNo">281</span>     * @return restricted and/or filtered view of this instance<a name="line.281"></a>
<span class="sourceLineNo">282</span>     * @throws IllegalArgumentException if {@code newBounds} is null<a name="line.282"></a>
<span class="sourceLineNo">283</span>     */<a name="line.283"></a>
<span class="sourceLineNo">284</span>    protected abstract NavigableSet&lt;E&gt; createSubSet(boolean newReversed,<a name="line.284"></a>
<span class="sourceLineNo">285</span>      KeyRange newKeyRange, KeyFilter newKeyFilter, Bounds&lt;E&gt; newBounds);<a name="line.285"></a>
<span class="sourceLineNo">286</span><a name="line.286"></a>
<span class="sourceLineNo">287</span>    /**<a name="line.287"></a>
<span class="sourceLineNo">288</span>     * Encode the given object into a {@code byte[]} key.<a name="line.288"></a>
<span class="sourceLineNo">289</span>     * Note that this method must throw {@link IllegalArgumentException}, not {@link ClassCastException}<a name="line.289"></a>
<span class="sourceLineNo">290</span>     * or {@code NullPointerException}, if {@code obj} does not have the correct type or is an illegal null value.<a name="line.290"></a>
<span class="sourceLineNo">291</span>     *<a name="line.291"></a>
<span class="sourceLineNo">292</span>     * @param writer output for encoded {@code byte[]} key corresponding to {@code obj}<a name="line.292"></a>
<span class="sourceLineNo">293</span>     * @param obj set element<a name="line.293"></a>
<span class="sourceLineNo">294</span>     * @throws IllegalArgumentException if {@code obj} is not of the required Java type supported by this set<a name="line.294"></a>
<span class="sourceLineNo">295</span>     * @throws IllegalArgumentException if {@code obj} is null and this set does not support null elements<a name="line.295"></a>
<span class="sourceLineNo">296</span>     */<a name="line.296"></a>
<span class="sourceLineNo">297</span>    protected abstract void encode(ByteWriter writer, Object obj);<a name="line.297"></a>
<span class="sourceLineNo">298</span><a name="line.298"></a>
<span class="sourceLineNo">299</span>    /**<a name="line.299"></a>
<span class="sourceLineNo">300</span>     * Decode an element from a {@code byte[]} key.<a name="line.300"></a>
<span class="sourceLineNo">301</span>     *<a name="line.301"></a>
<span class="sourceLineNo">302</span>     * &lt;p&gt;<a name="line.302"></a>
<span class="sourceLineNo">303</span>     * If not in prefix mode, all of {@code reader} must be consumed; otherwise, the consumed portion<a name="line.303"></a>
<span class="sourceLineNo">304</span>     * is the prefix and any following keys with the same prefix are ignored.<a name="line.304"></a>
<span class="sourceLineNo">305</span>     * &lt;/p&gt;<a name="line.305"></a>
<span class="sourceLineNo">306</span>     *<a name="line.306"></a>
<span class="sourceLineNo">307</span>     * @param reader input for encoded bytes<a name="line.307"></a>
<span class="sourceLineNo">308</span>     * @return decoded set element<a name="line.308"></a>
<span class="sourceLineNo">309</span>     */<a name="line.309"></a>
<span class="sourceLineNo">310</span>    protected abstract E decode(ByteReader reader);<a name="line.310"></a>
<span class="sourceLineNo">311</span><a name="line.311"></a>
<span class="sourceLineNo">312</span>    /**<a name="line.312"></a>
<span class="sourceLineNo">313</span>     * Determine if the given {@code byte[]} key is visible in this set according to the configured<a name="line.313"></a>
<span class="sourceLineNo">314</span>     * {@link KeyRange} and/or {@link KeyFilter}, if any.<a name="line.314"></a>
<span class="sourceLineNo">315</span>     *<a name="line.315"></a>
<span class="sourceLineNo">316</span>     * @param key key to test<a name="line.316"></a>
<span class="sourceLineNo">317</span>     * @return true if key is visible<a name="line.317"></a>
<span class="sourceLineNo">318</span>     * @throws IllegalArgumentException if {@code key} is null<a name="line.318"></a>
<span class="sourceLineNo">319</span>     * @see #filterKeys filterKeys()<a name="line.319"></a>
<span class="sourceLineNo">320</span>     */<a name="line.320"></a>
<span class="sourceLineNo">321</span>    protected boolean isVisible(byte[] key) {<a name="line.321"></a>
<span class="sourceLineNo">322</span>        return (this.keyRange == null || this.keyRange.contains(key))<a name="line.322"></a>
<span class="sourceLineNo">323</span>          &amp;&amp; (this.keyFilter == null || this.keyFilter.contains(key));<a name="line.323"></a>
<span class="sourceLineNo">324</span>    }<a name="line.324"></a>
<span class="sourceLineNo">325</span><a name="line.325"></a>
<span class="sourceLineNo">326</span>    /**<a name="line.326"></a>
<span class="sourceLineNo">327</span>     * Encode the given object, if possible, and verify corresponding {@code byte[]} key is visible,<a name="line.327"></a>
<span class="sourceLineNo">328</span>     * otherwise return null or throw an exception.<a name="line.328"></a>
<span class="sourceLineNo">329</span>     * Delegates to {@link #encode(ByteWriter, Object)} to attempt the actual encoding.<a name="line.329"></a>
<span class="sourceLineNo">330</span>     *<a name="line.330"></a>
<span class="sourceLineNo">331</span>     * @param obj object to encode, possibly null<a name="line.331"></a>
<span class="sourceLineNo">332</span>     * @param fail whether, if {@code obj} can't be encoded, to throw an exception (true) or return null (false)<a name="line.332"></a>
<span class="sourceLineNo">333</span>     * @return encoed key for {@code obj}, or null if {@code fail} is false and {@code obj} has the wrong type or is out of bounds<a name="line.333"></a>
<span class="sourceLineNo">334</span>     * @throws IllegalArgumentException if {@code fail} is true and {@code obj} has the wrong type<a name="line.334"></a>
<span class="sourceLineNo">335</span>     * @throws IllegalArgumentException if {@code fail} is true and the resulting key is not {@linkplain #isVisible visible}<a name="line.335"></a>
<span class="sourceLineNo">336</span>     */<a name="line.336"></a>
<span class="sourceLineNo">337</span>    protected byte[] encodeVisible(Object obj, boolean fail) {<a name="line.337"></a>
<span class="sourceLineNo">338</span>        final ByteWriter writer = new ByteWriter();<a name="line.338"></a>
<span class="sourceLineNo">339</span>        try {<a name="line.339"></a>
<span class="sourceLineNo">340</span>            this.encode(writer, obj);<a name="line.340"></a>
<span class="sourceLineNo">341</span>        } catch (IllegalArgumentException e) {<a name="line.341"></a>
<span class="sourceLineNo">342</span>            if (!fail)<a name="line.342"></a>
<span class="sourceLineNo">343</span>                return null;<a name="line.343"></a>
<span class="sourceLineNo">344</span>            throw e;<a name="line.344"></a>
<span class="sourceLineNo">345</span>        }<a name="line.345"></a>
<span class="sourceLineNo">346</span>        final byte[] key = writer.getBytes();<a name="line.346"></a>
<span class="sourceLineNo">347</span>        if (this.keyRange != null &amp;&amp; !this.keyRange.contains(key)) {<a name="line.347"></a>
<span class="sourceLineNo">348</span>            if (fail)<a name="line.348"></a>
<span class="sourceLineNo">349</span>                throw new IllegalArgumentException("value is out of bounds: " + obj);<a name="line.349"></a>
<span class="sourceLineNo">350</span>            return null;<a name="line.350"></a>
<span class="sourceLineNo">351</span>        }<a name="line.351"></a>
<span class="sourceLineNo">352</span>        if (this.keyFilter != null &amp;&amp; !this.keyFilter.contains(key)) {<a name="line.352"></a>
<span class="sourceLineNo">353</span>            if (fail)<a name="line.353"></a>
<span class="sourceLineNo">354</span>                throw new IllegalArgumentException("value is filtered out: " + obj);<a name="line.354"></a>
<span class="sourceLineNo">355</span>            return null;<a name="line.355"></a>
<span class="sourceLineNo">356</span>        }<a name="line.356"></a>
<span class="sourceLineNo">357</span>        return key;<a name="line.357"></a>
<span class="sourceLineNo">358</span>    }<a name="line.358"></a>
<span class="sourceLineNo">359</span><a name="line.359"></a>
<span class="sourceLineNo">360</span>    /**<a name="line.360"></a>
<span class="sourceLineNo">361</span>     * Derive a new {@link KeyRange} from (possibly) new element bounds. The given bounds must &lt;i&gt;not&lt;/i&gt; ever be reversed.<a name="line.361"></a>
<span class="sourceLineNo">362</span>     */<a name="line.362"></a>
<span class="sourceLineNo">363</span>    private KeyRange buildKeyRange(Bounds&lt;E&gt; bounds) {<a name="line.363"></a>
<span class="sourceLineNo">364</span>        final byte[] minKey = this.keyRange != null ? this.keyRange.getMin() : null;<a name="line.364"></a>
<span class="sourceLineNo">365</span>        final byte[] maxKey = this.keyRange != null ? this.keyRange.getMax() : null;<a name="line.365"></a>
<span class="sourceLineNo">366</span>        byte[] newMinKey;<a name="line.366"></a>
<span class="sourceLineNo">367</span>        byte[] newMaxKey;<a name="line.367"></a>
<span class="sourceLineNo">368</span>        switch (bounds.getLowerBoundType()) {<a name="line.368"></a>
<span class="sourceLineNo">369</span>        case NONE:<a name="line.369"></a>
<span class="sourceLineNo">370</span>            newMinKey = minKey;<a name="line.370"></a>
<span class="sourceLineNo">371</span>            break;<a name="line.371"></a>
<span class="sourceLineNo">372</span>        default:<a name="line.372"></a>
<span class="sourceLineNo">373</span>            final ByteWriter writer = new ByteWriter();<a name="line.373"></a>
<span class="sourceLineNo">374</span>            this.encode(writer, bounds.getLowerBound());<a name="line.374"></a>
<span class="sourceLineNo">375</span>            newMinKey = writer.getBytes();<a name="line.375"></a>
<span class="sourceLineNo">376</span>            if (!bounds.getLowerBoundType().isInclusive())<a name="line.376"></a>
<span class="sourceLineNo">377</span>                newMinKey = this.prefixMode ? ByteUtil.getKeyAfterPrefix(newMinKey) : ByteUtil.getNextKey(newMinKey);<a name="line.377"></a>
<span class="sourceLineNo">378</span>            if (minKey != null)<a name="line.378"></a>
<span class="sourceLineNo">379</span>                newMinKey = ByteUtil.max(newMinKey, minKey);<a name="line.379"></a>
<span class="sourceLineNo">380</span>            if (maxKey != null)<a name="line.380"></a>
<span class="sourceLineNo">381</span>                newMinKey = ByteUtil.min(newMinKey, maxKey);<a name="line.381"></a>
<span class="sourceLineNo">382</span>            break;<a name="line.382"></a>
<span class="sourceLineNo">383</span>        }<a name="line.383"></a>
<span class="sourceLineNo">384</span>        switch (bounds.getUpperBoundType()) {<a name="line.384"></a>
<span class="sourceLineNo">385</span>        case NONE:<a name="line.385"></a>
<span class="sourceLineNo">386</span>            newMaxKey = maxKey;<a name="line.386"></a>
<span class="sourceLineNo">387</span>            break;<a name="line.387"></a>
<span class="sourceLineNo">388</span>        default:<a name="line.388"></a>
<span class="sourceLineNo">389</span>            final ByteWriter writer = new ByteWriter();<a name="line.389"></a>
<span class="sourceLineNo">390</span>            this.encode(writer, bounds.getUpperBound());<a name="line.390"></a>
<span class="sourceLineNo">391</span>            newMaxKey = writer.getBytes();<a name="line.391"></a>
<span class="sourceLineNo">392</span>            if (bounds.getUpperBoundType().isInclusive())<a name="line.392"></a>
<span class="sourceLineNo">393</span>                newMaxKey = this.prefixMode ? ByteUtil.getKeyAfterPrefix(newMaxKey) : ByteUtil.getNextKey(newMaxKey);<a name="line.393"></a>
<span class="sourceLineNo">394</span>            if (maxKey != null)<a name="line.394"></a>
<span class="sourceLineNo">395</span>                newMaxKey = ByteUtil.min(newMaxKey, maxKey);<a name="line.395"></a>
<span class="sourceLineNo">396</span>            if (minKey != null)<a name="line.396"></a>
<span class="sourceLineNo">397</span>                newMaxKey = ByteUtil.max(newMaxKey, minKey);<a name="line.397"></a>
<span class="sourceLineNo">398</span>            break;<a name="line.398"></a>
<span class="sourceLineNo">399</span>        }<a name="line.399"></a>
<span class="sourceLineNo">400</span>        return new KeyRange(newMinKey != null ? newMinKey : ByteUtil.EMPTY, newMaxKey);<a name="line.400"></a>
<span class="sourceLineNo">401</span>    }<a name="line.401"></a>
<span class="sourceLineNo">402</span>}<a name="line.402"></a>
<span class="sourceLineNo">403</span><a name="line.403"></a>




























































</pre>
</div>
</body>
</html>
